import * as actionTypes from "./contstants.js";
import { getEntireRoomList } from "@/services/modules/entire";

export const changeCurrentPageAction = (currentPage) => {
  return {
    type: actionTypes.CHANGE_CURRENT_PAGE,
    currentPage: currentPage,
  };
};
export const changeRoomListAction = (roomList) => ({
  type: actionTypes.CHANGE_ROOM_LIST,
  roomList: roomList,
});
export const changeTotalCountAction = (totalCount) => ({
  type: actionTypes.CHANGE_TOTAL_COUNT,
  totalCount: totalCount,
});
export const changLoadingAction =(flag)=>({
  type:actionTypes.CHANGE_LOADING,
  isLoading:flag
})

export const fetchRoomListAction = (page=0) => {
  //   新函数
  return async (dispatch, getState) => {
    // 修改page
    dispatch(changeCurrentPageAction(page))
    // 修改loading
    dispatch(changLoadingAction(true))
    // const currentPage = getState().entire.currentPage;
    const pageSize = getState().entire.pageSize;
    const res = await getEntireRoomList(page * pageSize);
    const roomList = res.list;
    const totalCount = res.totalCount;

    dispatch(changLoadingAction(false))
    dispatch(changeRoomListAction(roomList));
    dispatch(changeTotalCountAction(totalCount));
  };
};
